/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#import "MainController.h"
#import "MailWindowController.h"
#import "MessageWindowService.h"
#import "PreferenceManager.h"
#import "CHBrowserService.h"
#import "NewAccountController.h"
#import "AccountManagerDlgController.h"

// This is a little ugly, but works.
extern const nsModuleComponentInfo* GetAppComponents(unsigned int * outNumComponents);

@interface MainController(Private)

-(void)syncMenuItems;

@end

@implementation MainController

-(id)init
{
  if ((self = [super init]))
  {
    mGeckoInitted = NO;
  }
  
  return self;
}

-(void)dealloc
{
  if ([[MessageWindowService sharedInstance] currentMailWindowController])
    [[[MessageWindowService sharedInstance] currentMailWindowController] release];
  
  [super dealloc];
}

#pragma mark -

-(void)syncMenuItems
{
  BOOL curState = [[PreferenceManager sharedInstance] getBooleanPref:"mail.inline_attachments" withSuccess:NULL];
  
  if (curState)
    [mInlineAttachmentsMenuItem setState:NSOnState];
  else
    [mInlineAttachmentsMenuItem setState:NSOffState];
  
  curState = [[PreferenceManager sharedInstance] getBooleanPref:"mailnews.message_display.disable_remote_image" withSuccess:NULL];
  if (curState)
    [mShowRemoteImagesMenuItem setState:NSOffState];
  else
    [mShowRemoteImagesMenuItem setState:NSOnState];
}

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  [self ensureGeckoInitted];
  [self syncMenuItems];
  [self showMailWindow];
}

-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
  return NSTerminateNow;
}

-(void)applicationWillTerminate: (NSNotification*)aNotification
{
  
}

-(NSMenu*)applicationDockMenu:(NSApplication *)sender
{
  return nil; //XXX note...
}

#pragma mark -

-(void)ensureGeckoInitted
{
  if (mGeckoInitted)
    return;
  
  // Bringing up the Preference Manager init's gecko.
  [PreferenceManager sharedInstance];
  
  // register our app components with the embed layer
  unsigned int numComps = 0;
  const nsModuleComponentInfo* comps = GetAppComponents(&numComps);
  CHBrowserService::RegisterAppComponents(comps, numComps);
  
  // To work around a bug on Tiger where the view hookup order has been changed from postfix to prefix
  // order, we need to set a user default to return to the old behavior.
  [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSViewSetAncestorsWindowFirst"];
  
  mGeckoInitted = YES;
}

#pragma mark -

-(void)showMailWindow
{
  // This is pretty ugly...
  if ([[MessageWindowService sharedInstance] currentMailWindowController])
    [[[MessageWindowService sharedInstance] currentMailWindowController] showWindow:self];
  else
    [[MessageWindowService sharedInstance] createAndShowMailWindowController];
}

-(BOOL)applicationShouldHandleReopen:(NSApplication*)theApp hasVisibleWindows:(BOOL)flag
{
  if (!flag)
    [self showMailWindow];
}

-(void)applicationDidBecomeActive:(NSNotification*)aNotification
{
}

#pragma mark -

//
// File Menu Actions
//

-(IBAction)newMessage:(id)sender
{
  [[[MessageWindowService sharedInstance] currentMailWindowController] newMailMessage:sender];
}

-(IBAction)newMailAccount:(id)sender
{
  [[NewAccountController sharedInstance] runNewAccountDialog:[[[MessageWindowService sharedInstance] currentMailWindowController] window]];
}

-(IBAction)openAccountManager:(id)sender
{
  [[AccountManagerDlgController sharedInstance] showAccountManager:[[[MessageWindowService sharedInstance] currentMailWindowController] window]];
}

//
// Edit Menu Actions
//

//
// Mail Menu Actions
//

-(IBAction)reply:(id)sender
{
  [[[MessageWindowService sharedInstance] currentMailWindowController] reply:self];
}

-(IBAction)replyAll:(id)sender
{
  [[[MessageWindowService sharedInstance] currentMailWindowController] replyAll:sender];
}

-(IBAction)forward:(id)sender
{
  [[[MessageWindowService sharedInstance] currentMailWindowController] foward:sender];
}

-(IBAction)sendReceive:(id)sender
{
  [[[MessageWindowService sharedInstance] currentMailWindowController] sendReceive:sender];
}

//
// View Menu Actions
//

-(IBAction)flipView:(id)sender
{
  [[[MessageWindowService sharedInstance] currentMailWindowController] setViewLayout:sender];
}

-(IBAction)toggleAttachmentsInline:(id)sender
{
  BOOL curState = [[PreferenceManager sharedInstance] getBooleanPref:"mail.inline_attachments" withSuccess:NULL];
  [[PreferenceManager sharedInstance] setPref:"mail.inline_attachments" toBoolean:!curState];
  [self syncMenuItems];
  [[[MessageWindowService sharedInstance] currentMailWindowController] reloadMessage:self];
}

-(IBAction)toggleShowRemoteImages:(id)sender
{
  BOOL curState = [[PreferenceManager sharedInstance] getBooleanPref:"mailnews.message_display.disable_remote_image" withSuccess:NULL];
  [[PreferenceManager sharedInstance] setPref:"mailnews.message_display.disable_remote_image" toBoolean:!curState];
  [self syncMenuItems];
  [[[MessageWindowService sharedInstance] currentMailWindowController] reloadMessage:self];
}

//
// Window Menu Actions
//
-(IBAction)showMailWindow:(id)sender
{
  [self showMailWindow];
}

//
// Main Menu Delegate
//
- (BOOL)validateMenuItem:(NSMenuItem*)aMenuItem
{
  return [[[MessageWindowService sharedInstance] currentMailWindowController] shouldEnableItemWithSelector:[aMenuItem action]];
}

@end
